package com.sqoq.downloader3.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.LinearLayoutCompat
import com.gyf.immersionbar.ImmersionBar
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.OnPermissionPageCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.sqoq.downloader3.R

class PermissionActivity_ddg : AppCompatActivity() {
    private var key_ddg: Int = 0

    var permissionList_ddg = ArrayList<String>()

    companion object {
        var listener = mutableMapOf<Int, (() -> Unit)>()
        fun start(context: Context, key_ddg: Int, hd: () -> Unit) {

            var intent = Intent(context, PermissionActivity_ddg::class.java)
            if (XXPermissions.isGranted(context, getPermissionList(key_ddg))) {
                hd?.invoke()
                return
            }
            listener[key_ddg] = hd
            intent.putExtra("key_ddg", key_ddg)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }

        fun getPermissionList(key_ddg: Int): ArrayList<String> {
            var permissionList = ArrayList<String>()
            if (key_ddg == 1) {
                permissionList = arrayListOf(
                    Permission.READ_MEDIA_IMAGES,
                    Permission.READ_MEDIA_AUDIO,
                    Permission.READ_MEDIA_VIDEO,
                    Permission.WRITE_EXTERNAL_STORAGE
                )
            }
//            else if (key_ddg == 2) {
//                permissionList = arrayListOf(
//                    Permission.POST_NOTIFICATIONS,
//                )
//            }
            return permissionList
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val params = window?.attributes
        params?.height = 1
        params?.gravity = Gravity.TOP
        window?.attributes = params
        setFinishOnTouchOutside(true)
        setContentView(LinearLayoutCompat(this))
        ImmersionBar.with(this)
            .navigationBarColor(R.color.white)
            .statusBarDarkFont(true)
            .flymeOSStatusBarFontColor(android.R.color.black) //修改 flyme OS 状态栏字体颜色
            .init()
        key_ddg = intent.getIntExtra("key_ddg", 0)
        permissionList_ddg = getPermissionList(key_ddg)


        val permissions: XXPermissions = XXPermissions.with(this)
        permissions.permission(
            permissionList_ddg
        )
        permissions.request(object : OnPermissionCallback {
            override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                if (allGranted) {
                    listener[key_ddg]?.invoke()

                }
                finish()
            }

            override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {


                if (doNotAskAgain) {
                    requestPermissions(doNotAskAgain, permissions)

                } else {
                    finish()
                }
            }
        })

    }

    fun requestPermissions(isDoNotAskAgainPermissions: Boolean, permissions: MutableList<String>) {

        if (isDoNotAskAgainPermissions) {
            XXPermissions.startPermissionActivity(
                this@PermissionActivity_ddg,
                permissions, object : OnPermissionPageCallback {
                    override fun onGranted() {
                        listener[key_ddg]?.invoke()
                        finish()
                    }

                    override fun onDenied() {
                        finish()
                    }
                }
            )

        } else {
            val permissions: XXPermissions = XXPermissions.with(this)
            permissions.permission(getPermissionList(key_ddg))
            permissions.request(object : OnPermissionCallback {
                override fun onGranted(permissions: List<String>, allGranted: Boolean) {
                    if (allGranted) {
                        listener[key_ddg]?.invoke()
                    }

                    finish()
                }

                override fun onDenied(permissions: List<String>, doNotAskAgain: Boolean) {
                    finish()
                }
            })
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        listener.remove(key_ddg)
    }


//    var s=false
//    override fun onResume() {
//        super.onResume()
//        if(s){
//            finish()
//        }
//        s=true
//    }

}